using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// 使用 C# 原生 LinkedList 的测试类
public class DNodeListLibraryTest
{
    public static void RunTest()
    {
        LinkedList<int> linkedList = new();

        Console.WriteLine("按 Enter 键继续测试...");
        Console.ReadLine();

        // 1. 头插（原生 AddFirst）
        linkedList.AddFirst(10);
        PrintLog(linkedList, "头插", "10");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 2. 尾插（原生 AddLast）
        linkedList.AddLast(20);
        PrintLog(linkedList, "尾插", "20");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 3. 指定位置插入
        int index = 1;
        int value = 30;

        if (index <= 0 || linkedList.Count == 0)
        {
            linkedList.AddFirst(value);
        }
        else if (index >= linkedList.Count)
        {
            linkedList.AddLast(value);
        }
        else
        {
            LinkedListNode<int> prevNode = linkedList.First;
            for (int i = 0; i < index - 1; i++)
            {
                prevNode = prevNode.Next;
            }
            linkedList.AddAfter(prevNode, value);
        }
        PrintLog(linkedList, "指定位置插入(1)", "30");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 4. 删头（原生 RemoveFirst）
        if (linkedList.Count > 0)
            linkedList.RemoveFirst();
        PrintLog(linkedList, "删头", "");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 5. 删尾（原生 RemoveLast）
        if (linkedList.Count > 0)
            linkedList.RemoveLast();
        PrintLog(linkedList, "删尾", "");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 6. 指定位置删除
        index = 1;
        if (index >= 0 && index < linkedList.Count)
        {
            LinkedListNode<int> targetNode = linkedList.First;
            for (int i = 0; i < index; i++)
            {
                targetNode = targetNode.Next;
            }
            linkedList.Remove(targetNode);
        }
        PrintLog(linkedList, "删除位置(1)", "");

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 7. 指定位置查询
        index = 1;
        int v = default;
        if (index >= 0 && index < linkedList.Count)
        {
            v = linkedList.ElementAt<int>(index);
        }
        Console.WriteLine("===============查询位置(1)====================");
        Console.WriteLine("值为:" + v);
        PrintNode(linkedList);

        Console.WriteLine("\n按 Enter 键继续测试...");
        Console.ReadLine();

        // 8. 指定位置修改
        index = 1;
        if (index >= 0 && index < linkedList.Count)
        {
            LinkedListNode<int> targetNode = linkedList.First;
            for (int i = 0; i < index; i++)
            {
                targetNode = targetNode.Next;
            }
            targetNode.Value = 99;
        }
        PrintLog(linkedList, "修改位置(1)为99", "");
    }

    // 统一打印日志
    private static void PrintLog(LinkedList<int> linkedList, string operation, string valueDesc)
    {
        Console.WriteLine($"=============== {operation} ====================");
        Console.WriteLine("当前链表长度为:" + linkedList.Count);
        if (!string.IsNullOrEmpty(valueDesc))
            Console.WriteLine("操作值: " + valueDesc);
        PrintNode(linkedList);
    }

    // 打印链表（原生遍历节点对象）
    private static void PrintNode(LinkedList<int> linkedList)
    {
        if (linkedList.Count == 0)
        {
            Console.WriteLine("链表内容: 空");
            return;
        }

        StringBuilder sb = new StringBuilder();
        LinkedListNode<int> currentNode = linkedList.First;
        while (currentNode != null)
        {
            sb.Append(currentNode.Value);
            if (currentNode.Next != null)
                sb.Append(" -> ");
            currentNode = currentNode.Next;
        }
        Console.WriteLine("链表内容: " + sb.ToString());
    }
}